Attribute VB_Name = "nodeQuery"

Public nqCtrl_Phase As Integer
Public nqCtrl_Wait As Integer
Public nqCtrl_Sent As Boolean
Public nqCtrl_RecID As Integer
Public nqCtrl_RecIsRelay() As Boolean
Public nqCtrl_RecSum As Integer
Public nqNodeBase() As String
Public nqCtrl_retry As Integer
Dim BannedIPList() As String

Public Sub nq_Phase_0_()
Dim tmp() As String, v As Long, i As Integer, xCount As Integer, gCount As Integer, tmpBadIP() As String
Dim vi As Integer, vs As Integer
            If Not nqCtrl_Sent Then
                SendCmd "GETINFO ns/all", False
                nqCtrl_Sent = True
            Else
                
                'not ready yet?
                If UBound(rQueue) < 3 Then Exit Sub
                
                Call load_BadIP
                Dim newORID As OrId
                Set newORID = New OrId
                    xCount = -1
                    gCount = -1
                    Init_GeoIp
                
                For i = 0 To UBound(rQueue)
                    Select Case Left$(rQueue(i), 1)
                    
                    Case "r"
                        tmp = Split(rQueue(i), " ")
                        vs = vs + 1
                        
                        'Country OK?
                        If tmp(6) = "" Then
                            Debug.Print Join(tmp, "|") + "|" + "-NoIP?"
                            GoTo SkipRec
                        End If
                        geoLookUp.find_by_addr (tmp(6))
                        If geoLookUp.country_code = "MO" Or _
                            geoLookUp.country_code = "CN" Or _
                            geoLookUp.country_code = "HK" _
                        Then Debug.Print tmp(1) + "|" + tmp(2) + "|" + geoLookUp.country_code + "-GeoBan": GoTo SkipRec
                                                
                        'Bandwidth ?
                        If InStr(1, rQueue(i + 1), "Valid") > 0 And _
                            InStr(1, rQueue(i + 1), "Running") > 0 And _
                            InStr(1, rQueue(i + 1), "Fast") > 0 Then

                                v = CLng(Split(rQueue(i + 2), "=")(1))
                                If v < 50 Then
                                    'Debug.Print Join(tmp, "|") + "|" + Str(v) + "-BWBan"
                                    vi = vi + 1
                                    GoTo SkipRec
                                End If
                        Else
                            GoTo SkipRec
                        End If
                        
                        'push the rec into database
                        If InStr(1, rQueue(i + 1), " Exit ") Then
                            xCount = xCount + 1
                            ReDim Preserve xBase(1, xCount)
                            xBase(0, xCount) = newORID.Base64ToHex(tmp(2) + "=")
                            xBase(1, xCount) = geoLookUp.country_code
                        Else
                            gCount = gCount + 1
                            ReDim Preserve gBase(2, gCount)
                            ReDim Preserve mBase(gCount)
                            gBase(2, gCount) = tmp(6)
                            gBase(0, gCount) = newORID.Base64ToHex(tmp(2) + "=")
                            gBase(1, gCount) = geoLookUp.country_code
                        'Blocked ? be my middle node
                            If IsBlockedIP(tmp(6)) Then
'                                Debug.Print Join(tmp, "|") + "-IPBan"
                                mBase(gCount) = True
                            End If
                        End If
                        
                            
 
 '                       End If

SkipRec:
                    End Select
                    If i > UBound(rQueue) - 10 Then logMan "#" + CStr(i), rQueue(i)
                Next i
Debug.Print Str(vi) + "-BWFilter|total =" + Str(vs)
                'clear the temporary varibles
                ReDim rQueue(0)
                Set newORID = Nothing
                Set geoLookUp = Nothing
                ReDim BannedIPList(0)
                
                nqCtrl_Sent = False
                
                ReDim nqCtrl_RecIsRelay(xCount)
                nqCtrl_RecSum = xCount
                nqCtrl_Sent = False
                
                nqCtrl_Phase = 1
                Call TorCtrl.NodeQuery_Timer
            End If
End Sub

Private Sub load_BadIP()
'IP which is for some reason just unreachable
    Dim s As String
  Open runPath + "\UnreachableIP.txt" For Binary As #3
    If Loc(3) = LOF(3) Then ReDim BannedIPList(0): Close #3: Exit Sub
    s = Input(LOF(3), 3)
    BannedIPList = Split(s, vbCrLf)
    Stat_BannedIPList = UBound(BannedIPList)
  Close #3
End Sub

Private Function IsBlockedIP(nIP As String) As Boolean
Dim i As Integer

For i = 0 To Stat_BannedIPList
    If BannedIPList(i) = nIP Then
        IsBlockedIP = True
        Exit For
    End If
Next i

End Function

Public Sub save_BadIP()
Dim s As Integer
Open runPath + "\UnreachableIP.txt" For Append As #3
    For i = 0 To UBound(gBase, 2)
        If gBadIP(i) Then
            s = s + 1
            Print #3, gBase(2, i)
        End If
    Next
    If s > 0 Then
        Print #3, "//" + CStr(s) + " Added at " + Format(Now(), "hh:mm mmm dd")
    End If
Close #3
End Sub

